package com.qfedu.buy.server.service.impl;

import com.qfedu.buy.common.config.RedisKeyConfig;
import com.qfedu.buy.common.dto.SkillActivityDto;
import com.qfedu.buy.common.util.RedissonUtil;
import com.qfedu.buy.common.vo.R;
import com.qfedu.buy.server.dao.SkillActivityDao;
import com.qfedu.buy.server.service.intf.SkillActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * @program: NeighborBuy
 * @description:
 * @author: Feri(邢朋辉)
 * @create: 2021-02-05 09:41
 */
@Service
public class SkillActivityServiceImpl implements SkillActivityService {
    @Autowired
    private SkillActivityDao dao;

    @Override
    public R queryLastV1() {
//        SkillActivityDto dto=dao.queryLast(System.currentTimeMillis()/1000);
        List<SkillActivityDto> list=dao.queryLastList(System.currentTimeMillis()/1000);
        if(list!=null){
            return R.ok(list);
        }else {
            return R.fail();
        }
    }
    //高频访问---缓存  失效
    @Override
    public R queryLastV2() {
        if(RedissonUtil.isHave(RedisKeyConfig.SKILL_ACTIVITY)){
            return R.ok(RedissonUtil.getList(RedisKeyConfig.SKILL_ACTIVITY));
        }else {
            List<SkillActivityDto> list=dao.queryLastList(System.currentTimeMillis()/1000);
            if(list!=null){
                RedissonUtil.setList(RedisKeyConfig.SKILL_ACTIVITY,list);
                RedissonUtil.expire(RedisKeyConfig.SKILL_ACTIVITY,RedisKeyConfig.SKILL_ACTIVITY_TIME);
                return R.ok(list);
            }
        }
        return R.fail();
    }
}